package com.example.monitoringsystem.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.monitoringsystem.bean.Doctor;
import com.example.monitoringsystem.bean.Patient;
import com.example.monitoringsystem.bean.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.monitoringsystem.mapper.DoctorMapper;
import com.example.monitoringsystem.mapper.PatientMapper;
import com.example.monitoringsystem.mapper.UserMapper;
import com.example.monitoringsystem.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {

    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PatientMapper patientMapper;


    @Override
    public List<Doctor> getAllDoctors() {
        QueryWrapper<Doctor> wrapper=new QueryWrapper<>();
        List<Doctor> list = doctorMapper.selectList(wrapper);
        return list;
    }

    @Override
    public Map getDoctorByDoctorId(int doctorId) {
        Map<String,Object> map=new HashMap<>();
        //根据医生id查询医生信息
        QueryWrapper<Doctor> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("doctor_id",doctorId);
        Doctor doctor = doctorMapper.selectOne(wrapper2);
        map.put("doctor",doctor);

        //根据医生id查询用户信息
        QueryWrapper<User> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("id",doctorId);
        User user = userMapper.selectOne(wrapper3);
        map.put("user",user);
        return map;
    }


    @Override
    public void updateDoctor(User user,Doctor doctor) {
        userMapper.updateById(user);//更新用户表*/
        if(doctor.getPosition()==null){return;}
        doctor.setDoctorId(user.getId());
        doctorMapper.updateById(doctor);//更新doctor表信息
    }

    @Override
    public int deleteDoctor(int doctorId) {
        userMapper.deleteById(doctorId);//将医生的用户信息删除
        return doctorMapper.deleteById(doctorId);//将医生信息删除
    }

    @Override
    public List<Patient> getAllPatients() {
        QueryWrapper<Patient> wrapper=new QueryWrapper<>();
        List<Patient> list = patientMapper.selectList(wrapper);
        return list;
    }

    @Override
    public Map getPatientByPatientId(int patientId) {
        Map<String,Object> map=new HashMap<>();
        //根据医生id查询医生信息
        QueryWrapper<Patient> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("doctor_id",patientId);
        Patient patient = patientMapper.selectOne(wrapper2);
        map.put("patient",patient);

        //根据医生id查询用户信息
        QueryWrapper<User> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("id",patientId);
        User user = userMapper.selectOne(wrapper3);
        map.put("user",user);
        return map;
    }

    @Override
    public void updatePatient(User user) {
        userMapper.updateById(user);
    }

    @Override
    public int deletePatient(int patientId) {
        userMapper.deleteById(patientId);//将病人的用户信息删除
        return userMapper.deleteById(patientId);//将病人信息删除
    }

    @Override
    public List<User> getAllDoctorUsers() {
/*        QueryWrapper<Doctor> wrapper=new QueryWrapper<>();
        wrapper.select("doctor_id");
        List<Object> list = doctorMapper.selectObjs(wrapper);*/
        QueryWrapper<User> wrapper1=new QueryWrapper<>();
        wrapper1.eq("role","doctor");
        return userMapper.selectList(wrapper1);
    }

    @Override
    public void register(User user) {
        if(user.getRole()==null || user.getRole().isEmpty()){
            System.out.println("注册失败");
            return;
        }
        userMapper.insert(user);
        switch (user.getRole()){
            case "patient":
                Patient patient = new Patient();
                patient.setPatientId(user.getId());
                patientMapper.insert(patient);
                break;
            case "doctor":
                Doctor doctor = new Doctor();
                doctor.setDoctorId(user.getId());
                doctorMapper.insert(doctor);
                break;

            default:
                break;
        }
    }

}
